package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品接口")
@Slf4j
public class DishController {



  @Autowired
  DishService dishService;

  /**
   * 新增菜品
   * @param dto
   * @return
   */
  @PostMapping
  @ApiOperation("新增菜品")
  public Result save(@RequestBody DishDTO dto){
    log.info("新增菜品：{}", dto);
    dishService.saveWithFlavor(dto);
    return Result.success();
  }

  /**
   * 菜品分页查询
   * @param dto
   * @return
   */
  @GetMapping("/page")
  @ApiOperation("菜品分页查询")
  public Result<PageResult> page(DishPageQueryDTO dto){
    log.info("菜品分页查询：{}", dto);
    PageResult pageResult = dishService.pageQuery(dto);
    return Result.success(pageResult);
  }

  /**
   * 菜品批量删除
   * @param ids
   * @return
   */
  @DeleteMapping
  @ApiOperation("菜品批量删除")
  public Result delete(@RequestParam List<Long> ids){
    log.info("菜品分类删除:{}", ids);
    dishService.deleteBatch(ids);
    return Result.success();
  }

  /**
   * 根据id查询菜品
   * @param id
   * @return
   */
  @GetMapping("/{id}")
  @ApiOperation("根据id查询菜品")
  public Result<DishVO> getById(@PathVariable Long id){
    log.info("根据id获取数据:{}", id);
    DishVO dishVO = dishService.getByIdWithFlavor(id);
    return Result.success(dishVO);

  }

  /**
   * 更新菜品信息
   * @param dto
   * @return
   */
  @PutMapping
  @ApiOperation("更新菜品信息")
  public Result update(@RequestBody DishDTO dto){
    log.info("更新菜品信息:{}", dto);
    dishService.updateWithFlavor(dto);
    return Result.success();
    
  }

  /**
   * 菜品起售停售
   * @param status
   * @param id
   * @return
   */
  @PostMapping("status/{status}")
  @ApiOperation("菜品起售停售")
  public Result startOrStop(@PathVariable Integer status, Long id){
    log.info("菜品起售停售 status{},id:{}", status, id);
    dishService.startOrStop(status,  id);
    return Result.success();
  }
}

